﻿#include "TableStructs.h"
#include "MemCacheTemplateSingleton.h"

using namespace std;


Tick* Tick::Allocate()
{
	return ::Allocate<Tick>();
}
void Tick::Free()
{
	::Free(this);
}
std::string Tick::GetHeadString()
{
	return std::string("TradingDay,ExchangeID,InstrumentID,UpdateTs,LastPrice,LastTraded,Volume,OpenInterest,Turnover,BidPrice1,BidVolume1,AskPrice1,AskVolume1,PreSettlementPrice,PreClosePrice,PreOpenInterest,HighestPrice,LowestPrice\n");
}
std::string Tick::CreateDuckdbTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Tick(TradingDay bigint, ExchangeID varchar, InstrumentID varchar, UpdateTs bigint, LastPrice decimal(24,8), LastTraded double, Volume double, OpenInterest double, Turnover decimal(24,8), BidPrice1 decimal(24,8), BidVolume1 double, AskPrice1 decimal(24,8), AskVolume1 double, PreSettlementPrice decimal(24,8), PreClosePrice decimal(24,8), PreOpenInterest double, HighestPrice decimal(24,8), LowestPrice decimal(24,8), PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)); CREATE INDEX IF NOT EXISTS InstrumentIDIndex ON t_Tick(InstrumentID);";
}
std::string Tick::CreateSqliteTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Tick(TradingDay bigint, ExchangeID char(8), InstrumentID char(32), UpdateTs bigint, LastPrice decimal(24,8), LastTraded double, Volume double, OpenInterest double, Turnover decimal(24,8), BidPrice1 decimal(24,8), BidVolume1 double, AskPrice1 decimal(24,8), AskVolume1 double, PreSettlementPrice decimal(24,8), PreClosePrice decimal(24,8), PreOpenInterest double, HighestPrice decimal(24,8), LowestPrice decimal(24,8), PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)); CREATE INDEX IF NOT EXISTS InstrumentIDIndex ON t_Tick(InstrumentID);";
}
std::string Tick::CreateMysqlTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Tick(TradingDay bigint, ExchangeID char(8), InstrumentID char(32), UpdateTs bigint, LastPrice decimal(24,8), LastTraded double, Volume double, OpenInterest double, Turnover decimal(24,8), BidPrice1 decimal(24,8), BidVolume1 double, AskPrice1 decimal(24,8), AskVolume1 double, PreSettlementPrice decimal(24,8), PreClosePrice decimal(24,8), PreOpenInterest double, HighestPrice decimal(24,8), LowestPrice decimal(24,8), INDEX InstrumentID(InstrumentID), PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)) ENGINE=MyISAM DEFAULT COLLATE='utf8mb4_bin';";
}
std::string Tick::TruncateDuckdbSqlString()
{
	return "Delete From t_Tick;";
}
std::string Tick::TruncateSqliteSqlString()
{
	return "Delete From t_Tick;";
}
std::string Tick::TruncateMysqlSqlString()
{
	return "Truncate Table t_Tick;";
}

int Tick::ToCsvString(char* buff) const
{
	return sprintf(buff, "\n('%lld','%s','%s','%lld','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
}
int Tick::ToDebugString(char* buff) const
{
	return sprintf(buff, "\n(TradingDay:'%lld', ExchangeID:'%s', InstrumentID:'%s', UpdateTs:'%lld', LastPrice:'%f', LastTraded:'%f', Volume:'%f', OpenInterest:'%f', Turnover:'%f', BidPrice1:'%f', BidVolume1:'%f', AskPrice1:'%f', AskVolume1:'%f', PreSettlementPrice:'%f', PreClosePrice:'%f', PreOpenInterest:'%f', HighestPrice:'%f', LowestPrice:'%f'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
}
int Tick::GetSqlString(char* buff) const
{
	return sprintf(buff, "\n('%lld', '%s', '%s', '%lld', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, LastPrice, LastTraded, Volume, OpenInterest, Turnover, BidPrice1, BidVolume1, AskPrice1, AskVolume1, PreSettlementPrice, PreClosePrice, PreOpenInterest, HighestPrice, LowestPrice);
}

Bar* Bar::Allocate()
{
	return ::Allocate<Bar>();
}
void Bar::Free()
{
	::Free(this);
}
std::string Bar::GetHeadString()
{
	return std::string("TradingDay,ExchangeID,InstrumentID,UpdateTs,Prece,LastTraded,Volume,Turnover,OpenInterest,Open,High,Low,Close,HighestPrice,LowestPrice,ProductClass,UnderlyingID\n");
}
std::string Bar::CreateDuckdbTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Bar(TradingDay bigint, ExchangeID varchar, InstrumentID varchar, UpdateTs bigint, Prece varchar, LastTraded double, Volume double, Turnover double, OpenInterest double, Open decimal(24,8), High decimal(24,8), Low decimal(24,8), Close decimal(24,8), HighestPrice decimal(24,8), LowestPrice decimal(24,8), ProductClass int, UnderlyingID varchar, PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)); CREATE INDEX IF NOT EXISTS InstrumentIDIndex ON t_Bar(TradingDay, ExchangeID, InstrumentID);CREATE INDEX IF NOT EXISTS TradingDayIndex ON t_Bar(TradingDay);";
}
std::string Bar::CreateSqliteTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Bar(TradingDay bigint, ExchangeID char(8), InstrumentID char(32), UpdateTs bigint, Prece char(8), LastTraded double, Volume double, Turnover double, OpenInterest double, Open decimal(24,8), High decimal(24,8), Low decimal(24,8), Close decimal(24,8), HighestPrice decimal(24,8), LowestPrice decimal(24,8), ProductClass int, UnderlyingID char(32), PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)); CREATE INDEX IF NOT EXISTS InstrumentIDIndex ON t_Bar(TradingDay, ExchangeID, InstrumentID);CREATE INDEX IF NOT EXISTS TradingDayIndex ON t_Bar(TradingDay);";
}
std::string Bar::CreateMysqlTableSqlString()
{
	return "CREATE TABLE IF NOT EXISTS t_Bar(TradingDay bigint, ExchangeID char(8), InstrumentID char(32), UpdateTs bigint, Prece char(8), LastTraded double, Volume double, Turnover double, OpenInterest double, Open decimal(24,8), High decimal(24,8), Low decimal(24,8), Close decimal(24,8), HighestPrice decimal(24,8), LowestPrice decimal(24,8), ProductClass int, UnderlyingID char(32), INDEX InstrumentID(TradingDay, ExchangeID, InstrumentID), INDEX TradingDay(TradingDay), PRIMARY KEY(TradingDay, ExchangeID, InstrumentID, UpdateTs)) ENGINE=MyISAM DEFAULT COLLATE='utf8mb4_bin';";
}
std::string Bar::TruncateDuckdbSqlString()
{
	return "Delete From t_Bar;";
}
std::string Bar::TruncateSqliteSqlString()
{
	return "Delete From t_Bar;";
}
std::string Bar::TruncateMysqlSqlString()
{
	return "Truncate Table t_Bar;";
}

int Bar::ToCsvString(char* buff) const
{
	return sprintf(buff, "\n('%lld','%s','%s','%lld','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%d','%s'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
}
int Bar::ToDebugString(char* buff) const
{
	return sprintf(buff, "\n(TradingDay:'%lld', ExchangeID:'%s', InstrumentID:'%s', UpdateTs:'%lld', Prece:'%s', LastTraded:'%f', Volume:'%f', Turnover:'%f', OpenInterest:'%f', Open:'%f', High:'%f', Low:'%f', Close:'%f', HighestPrice:'%f', LowestPrice:'%f', ProductClass:'%d', UnderlyingID:'%s'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
}
int Bar::GetSqlString(char* buff) const
{
	return sprintf(buff, "\n('%lld', '%s', '%s', '%lld', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%d', '%s'),",
		TradingDay, ExchangeID, InstrumentID, UpdateTs, Prece, LastTraded, Volume, Turnover, OpenInterest, Open, High, Low, Close, HighestPrice, LowestPrice, (int)ProductClass, UnderlyingID);
}

